package com.lzh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.ThreadLocalUtils;
import com.lzh.bean.SupplierInquiry;
import com.lzh.bean.SupplierInquiryDetail;
import com.lzh.bean.SupplierQuotation;
import com.lzh.common.bean.PageBean;
import com.lzh.common.utils.Assert;
import com.lzh.common.utils.DateUtil;
import com.lzh.mapper.SupplierInquiryDetailMapper;
import com.lzh.mapper.SupplierInquiryMapper;
import com.lzh.mapper.SupplierQuotationMapper;
import com.lzh.query.SupplierInquiryQuery;
import com.lzh.service.SupplierInquiryService;
import com.lzh.service.SupplierSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SupplierInquiryServiceImpl implements SupplierInquiryService {

    //询价单前缀
    String INQUIRY_CODE_BEFORE="CPXJ";
    //供应商询价单最大编码
    String maxCode;

    @Autowired
    SupplierInquiryMapper supplierInquiryMapper;

    @Autowired
    SupplierInquiryDetailMapper supplierInquiryDetailMapper;

    @Autowired
    SupplierQuotationMapper    supplierQuotationMapper;
    /**
     * 保存询价单
     * @param supplierInquiry
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(SupplierInquiry supplierInquiry) {


        //获取当前销售
        Map map = ThreadLocalUtils.getInstance().get();
        int id = Integer.parseInt(map.get("id").toString());
        String name = map.get("username").toString();
        supplierInquiry.setInquiryId(id);
        supplierInquiry.setInquiryName(name);
        //自动生成供应商询价单编码
        supplierInquiry.setCode(getInquiryCode());

        //保存供应商询价单
        int save = supplierInquiryMapper.save(supplierInquiry);

        //保存明细
        //获取供应商询价单

        if (supplierInquiry.getSupplierInquiryDetail()!=null && supplierInquiry.getSupplierInquiryDetail().size()>0){
            List<SupplierInquiryDetail> supplierInquiryDetail = supplierInquiry.getSupplierInquiryDetail();
            for (SupplierInquiryDetail inquiryDetail : supplierInquiryDetail) {
                inquiryDetail.setInquiryId(supplierInquiry.getId());
              int save1 = supplierInquiryDetailMapper.save(inquiryDetail);
                //保存询价单明细之后
                //根据产品id找到生产此类型的供应商id
                //供应商表中的产品主营分类
                //对应产品表
                List<Integer> integers = supplierQuotationMapper.queryByPid(inquiryDetail.getPid());
                if (integers!=null&&integers.size()>0){
                    for (int i = 0, len = integers.size(); i < len; i++) {
                        String tmp = supplierInquiry.getCode()+(i+1);
                        //保存报价表
                        int save2 = supplierQuotationMapper.save(tmp, inquiryDetail.getPid(), inquiryDetail.getInquiryId(),integers.get(i));
                        Assert.isZerp(save2,"保存供应商报价单失败");
                    }
                }

            }


        }

        return 1;
    }

    /**
     * 查询询价订单
     * @param supplierInquiryQuery
     * @return
     */
    @Override
    public PageBean query(SupplierInquiryQuery supplierInquiryQuery) {

        PageHelper.startPage(supplierInquiryQuery.getPage(),supplierInquiryQuery.getPageSize());
        List query = supplierInquiryMapper.query(supplierInquiryQuery);
        PageInfo pageInfo = new PageInfo(query);
        return new PageBean<>(supplierInquiryQuery.getPage(),supplierInquiryQuery.getPageSize(),pageInfo.getTotal(),query);
    }


    /**
     * 通过id查询询价单
     * @param id
     * @return
     */
    @Override
    public SupplierInquiry queryById(int id) {
        SupplierInquiry supplierInquiry = supplierInquiryMapper.queryById(id);
        return supplierInquiry;
    }


    /**
     * EN20230202CM-01A
     * EN+年月日（20201212）+销售名称前两个字母大写+询价单数量（当天第几单）+A
     * 未答复时间、报价耗时  计算公司参考表格中公式
     */
    String  getInquiryCode(){
        String ym = DateUtil.getSystemTimeYm();
        //内存中不存在最大编码
        if (maxCode==null){
            //查询最大编码
            maxCode = supplierInquiryMapper.queryMaxCode();
            if (maxCode==null){
                //如果最大编码是空的话
                //设置一个自定义的编码
                //询价单前缀+当前年月+0001
                maxCode=INQUIRY_CODE_BEFORE+ ym  + "0001";
                return maxCode;
            }
        }

        //内存中存在最大编码
        //判断是否是当月的
        //CPXJYYMM0001
        if (maxCode.substring(4,8).equals(ym)){
            //是当月的，后面的数+1
            int code = Integer.parseInt(maxCode.substring(8));
            code = code + 1;
            String result = String.format("%04d", code);
            maxCode = maxCode.substring(0, 8) + result;
            return maxCode;
        }

        //不是当月的，判断是否为最大编码
        maxCode=INQUIRY_CODE_BEFORE+ ym + "0001";
        return maxCode;
    }

}
